package com.service.store.impl;

import com.dao.store.CatalogDao;
import com.domain.store.Catalog;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.store.CatalogService;
import com.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class CatalogServiceImpl implements CatalogService {
    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Catalog> findAll() {
        List<Catalog> list = null;
        try {
            SqlSession session= MapperUtils.init();
            CatalogDao mapper = session.getMapper(CatalogDao.class);
            list = mapper.findAll();
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findAll(int page, int size) {
        PageInfo pageInfo = null;
        try {
            SqlSession session= MapperUtils.init();
            CatalogDao mapper = session.getMapper(CatalogDao.class);
            Page pages = PageHelper.startPage(page,size);
            mapper.findAll();
            pageInfo = new PageInfo(pages);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Catalog findById(String id) {
        Catalog catalog = null;
        try {
            SqlSession session= MapperUtils.init();
            CatalogDao mapper = session.getMapper(CatalogDao.class);
            catalog = mapper.findById(id);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return catalog;
    }

    /**
     * 添加操作
     * @param catalog
     */
    @Override
    public void save(Catalog catalog) {
        try {
            SqlSession session= MapperUtils.init();
            CatalogDao mapper = session.getMapper(CatalogDao.class);
            String id = UUID.randomUUID().toString();
            catalog.setId(id);
            mapper.save(catalog);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**删除操作
     * @param catalog
     */
    @Override
    public void delete(Catalog catalog) {
        try {
            SqlSession session= MapperUtils.init();
            CatalogDao mapper = session.getMapper(CatalogDao.class);
            mapper.delete(catalog);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改操作
     * @param catalog
     */
    @Override
    public void update(Catalog catalog) {
        try {
            SqlSession session= MapperUtils.init();
            CatalogDao mapper = session.getMapper(CatalogDao.class);
            mapper.update(catalog);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
